# test reading sentences from a file and converting them to parsed_sentences/facts
import os
import sys, traceback

import unittest

import test.file
import rai.tagger.sentencereader as sentencereader
from rai.sem.FOL import *

#Suzie know Hamlet
# know(Suzie, Hamlet)

# "Who knows Suzie"
#  * Bello
# know(Bello, Suzie)
# know(?, Suzie)

#line = f.readline()
#parsed_sentence = parser.parseSentence(line)
#print parsed_sentence
#information = world.addFact(parsed_sentence)
#for p in parser.parseSentence(line):
#    print p
#print information
#print world.answerQuestion("Who is a singer")
class AnswerQuestionTestCase(unittest.TestCase):
    """
        Tests the world.answerQuestion method
        World.answerQuestion uses a valuation file to store its facts about the world
    """
    
    
    def setUp(self):
        __dir__ = test.file.__path__
        file_location = __dir__[0] + "\\test.facts.txt"
        file_location = __dir__[0] + "\\eventfacts.txt"    
        event_fcfg = "file:" + __dir__[0] + "\\org.event.fcfg"
        
        self.event_facts = file_location
        self.event_grammar_file = event_fcfg
        
    def testFullText(self):
        reader = sentencereader.SentenceReader()
        parsedSentences = reader.parseFile(filename=self.event_facts, maxlines=2,collapse_PP=False)

        event_val = None
        world = World(grammar_file=self.event_grammar_file, val_file=event_val)
    
        exceptions = []
        for parsed_sentence in parsedSentences:
            print parsed_sentence
            info = world.addFact(parsed_sentence)
            print info
            """
                #s [['VP', 'to'], ['VP', 'have', 'given']]
                #s [['VP', ('to', 'TO', 'TO')], ['VP', ('have', 'HV', 'VBP'), ('given', 'VBN', 'VBN')]]
            except Exception, inst:
                print inst
                traceback.print_exc(file=sys.stdout)
                exceptions.append(inst)
            lastLine = line   
            line = f.readline()
            i = i + 1
            """
        
        for e in exceptions:
            print e
            
        world.writeWorld("world.test")
        #for p in parser.parseSentence(lastLine):
        #    print "parse: " + str(p)
        #world.answerQuestion("Who is a singer", trace = 5)
    
        #\y.exists x.((kitchen(x) & loves(y,x)) & all z3.(kitchen(z3) -> (x = z3)))
        #world.answerQuestion("Who loves the kitchen", trace = 5)
        
        #\y.exists x.(kitchen(x) & loves(y,x))
        #world.answerQuestion("Who loves a kitchen", trace = 5)
        
        #\y.all x.(kitchen(x) -> loves(y,x))
        #world.answerQuestion("Who loves all kitchens", trace = 5)
       
        #world.answerQuestion("Who loves Bob", trace = 5)
        #world.answerQuestion("who chases Angus", trace = 5)
        world.answerQuestion("Pat walks", trace = 5)
           
        #world.answerQuestion("Who is happy", trace = 0)
        #world.answerQuestion("Who is drunk", trace = 0)
        #world.answerQuestion("Who is in Noosa", trace = 0)
        #world.answerQuestion("Who loves Suzie", trace = 0)
        #world.answerQuestion("Who loves Jim", trace = 5) # TODO: add Jim as PropN to grammar-rules
        
        
        #<LambdaExpression \y.know(y,Suzie)>
        #world.answerQuestion("Who knows Suzie", trace = 5)
        
        #world.answerQuestion("Who knows Bob", trace = 5)
        
        #world.answerQuestion("Who performs Hamlet", trace = 5)
    
        #<LambdaExpression \y.exists x.((kitchen(x) & loves(y,x)) & all z3.(kitchen(z3) -> (x = z3)))>
        #world.answerQuestion("Who performs the musical", trace = 5)
        #world.answerLogic("exists y1.(musical(y1))")
        #world.satifiers("exists y1.(musical(y1))")
        #<LambdaExpression \y.exists x.((musical(x) & performs(y,x)) & all z3.(musical(z3) -> (x = z3)))>}
        #world.answerQuestion("Who performs the musical", trace = 5)
        
        
        #<LambdaExpression \y.exists x.(musical(x) & performs(y,x))>
        #world.answerQuestion("Who performs a musical", trace = 5)
        
        # <LambdaExpression \y.exists x.((musical(x) & performs(y,x)) & all z3.(musical(z3) -> (x = z3)))>
        #world.answerLogic("\y.musical(y)")
        #world.answerQuestion("Who performs Suzie", trace = 5)
        
        #world.answerLogic("\y.know(y,Suzie)")
        #world.answerLogic("\y.exists x.(girl(x) & know(y,x))")
        
        #performs -> [TV[num='sg', sem=<\X.\y.X(\x.performs(y,x))>, tns='pres']]
        # VP[num=?n,sem=<app(?v,?obj)>] -> TV[num=?n,sem=?v] NP[sem=?obj]
        # \X.\y.X(\x.performs(y,x))
        # \y.exists x.(musical(x) & performs(y,x))
        #world.answerLogic("\y.performs(y, musical)")
        
        #world.answerQuestion("Who performs Suzie", trace = 5)
    
#parsed_sentence = parser.parseSentence(line)
#for part in parsed_sentence:
#    print part

def testSimple():
    world = World()
    parser = SentenceParser()
    line = "Suzie loves Mary"
    parsed_sentence = parser.parseSentence(line)
    info = world.addFact(parsed_sentence)
    for p in parser.parseSentence(line):
        #print p
        pass    
    print info    
    world.writeWorld("world.test")
    world.answerQuestion("who loves a girl")
    #print world.answerQuestion("John sees a girl")
    #logic = "\\x.loves(x,Mary)"
    #logic = "\\x.loves(x,Mary)"
    #logic = "\\x.exists z1.(girl(z1) & loves(x,z1))"
    #logic = "exists x.(girl(x) & see(John,x))"
    #world.answerLogic(logic)
    #world.satifiers("", "")
    
#testSimple()
if __name__ == "__main__":
    print "main"
    unittest.main()
#testFullText()